﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public partial class ProcessSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid(Request.QueryString["MissileMode"].Trim(),Request.QueryString["RepairType"].Trim());
            }
        }

        private void BindGrid(string missileModel,string repairType)
        {
            DeptOB deptOB = CurrentInfo.Instance.CurrentUser.Dept;
            string strWhere = string.Empty;
            if (deptOB != null)
            {
                strWhere = string.Format("AND DeptID={0}", deptOB.DeptID);
            }
            else
            {
                strWhere = string.Empty;
            }

            string sql = @"SELECT * FROM dbo.t5715_Process WHERE MissileModel = @MissileModel {0} ORDER BY ProcessID";
            sql = string.Format(sql, strWhere);

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@MissileModel", DbType.String, missileModel);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gvProcess.DataSource = dataTable;
            gvProcess.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
         
            IList<string> processIDs = new List<string>(); 
            foreach (GridViewRow gridViewRow in gvProcess.Rows)
            {
                if (gridViewRow.RowType != DataControlRowType.DataRow)
                {
                    continue;
                }
                CheckBox checkBox = gridViewRow.Cells[0].FindControl("ckbSelect") as CheckBox;
                if (checkBox == null)
                {
                    continue;
                }

                if (checkBox.Checked)
                {
                    processIDs.Add(gvProcess.DataKeys[gridViewRow.RowIndex]["ProcessID"].ToString());
                }
            }
            AddProccess(processIDs);

            Response.Write("<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
        }

        private void AddProccess(IList<string> list_ProccessID)
        {
            string DispatchMissileID = Request.QueryString["DispatchMissileID"].Trim();

            var dal = new ProcessDal();

            //获得工序最大排序号
            int MaxOrder = dal.GetMaxProcessOrderNumber(DispatchMissileID);

            List<DbCommand> dbCommandList = new List<DbCommand>();
            Database database = DatabaseFactory.CreateDatabase();
            foreach (string  ProcessID in list_ProccessID)
            {
                MaxOrder++;
                DispatchMissileProcessOB dispatchMissileProcessOB = new DispatchMissileProcessOB();
                dispatchMissileProcessOB.DispatchMissileProcessID = Guid.NewGuid().ToString();
                dispatchMissileProcessOB.DispatchMissileID = DispatchMissileID;
                dispatchMissileProcessOB.ProcessID = ProcessID;
                dispatchMissileProcessOB.ProcessSortNumber = MaxOrder.ToString();
                dispatchMissileProcessOB.IsBeginRepair = "0";
               
                DbCommand dbCommand = dal.GetDbCommand_DispatchMissileProcess_Insert(database, dispatchMissileProcessOB);
                dbCommandList.Add(dbCommand);
            }

            DbConnection dbConnection = null;
            DbTransaction dbTransaction = null;
           
            dbConnection = database.CreateConnection();
            dbConnection.Open();
            dbTransaction = dbConnection.BeginTransaction();

            try
            {
                foreach (DbCommand dbCmd in dbCommandList)
                {
                    database.ExecuteNonQuery(dbCmd, dbTransaction);
                }

                dbTransaction.Commit();                
            }
            catch (Exception)
            {
                dbTransaction.Rollback();
                throw;
            }
         
        }

        protected void gvProcess_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            string deptID = gvProcess.DataKeys[e.Row.RowIndex]["DeptID"].ToString();
            e.Row.Cells[4].Text = new UserPermission.UserPermissionBll().GetDeptFullName(deptID);
        }
    }
}